#include "searchitem.h"

#include "ui_searchitem.h"

#include <Application.h>
#include <CommonUtils.h>

SearchItem::SearchItem(QWidget* parent) : QWidget(parent),
                                          ui(new Ui::SearchItem) {
    ui->setupUi(this);
    connect(Application::instance(), &Application::signalAddFriends, this, &SearchItem::onAddFriends);
}

SearchItem::~SearchItem() {
    delete ui;
}

void SearchItem::setInfo(std::shared_ptr<::proto::data::UserInfo> info) {
    this->info    = info;
    
    ui->label_head->setHeadshot(QString::fromStdString(info->headshot()));
    ui->label_name->setText(QString::fromStdString(info->name()));
    ui->label_userid->setText(QString::fromStdString(info->userid()));

    auto users = Application::instance()->firends();
    for (auto&& i : users) {
        if (i->userid() == info->userid()) {
            ui->btn_add->setText("已添加");
            ui->btn_add->setEnabled(false);
        }
    }
}

void SearchItem::onAddFriends(int code, QString message, std::shared_ptr<::proto::data::FriendInfo> info) {
    if (code) {
        return;
    }

    if (info->userid() == this->info->userid()) {
        ui->btn_add->setText("已添加");
        ui->btn_add->setEnabled(false);
    }
}

void SearchItem::on_btn_add_clicked() {
    Application::instance()->AddFriends(info->userid());
}
